home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / info / diff.info-1 (.txt) < prev    next >
GNU Info File  |  1995-07-29  |  50KB  |  963 lines

  1. This is Info file diff.info, produced by Makeinfo-1.55 from the input
  2. file ./diff.texi.
  3.    This file documents the the GNU `diff', `diff3', `sdiff', and `cmp'
  4. commands for showing the differences between text files and the `patch'
  5. command for using their output to update files.
  6.    Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  7.    Permission is granted to make and distribute verbatim copies of this
  8. manual provided the copyright notice and this permission notice are
  9. preserved on all copies.
  10.    Permission is granted to copy and distribute modified versions of
  11. this manual under the conditions for verbatim copying, provided that
  12. the entire resulting derived work is distributed under the terms of a
  13. permission notice identical to this one.
  14.    Permission is granted to copy and distribute translations of this
  15. manual into another language, under the above conditions for modified
  16. versions, except that this permission notice may be stated in a
  17. translation approved by the Foundation.
  18. File: diff.info,  Node: Top,  Up: (dir)
  19.    This file documents the the GNU `diff', `diff3', `sdiff', and `cmp'
  20. commands for showing the differences between text files and the `patch'
  21. command for using their output to update files.
  22.    This is Edition 1.0, for `diff' 2.1 and `patch' 2.0.12g8.
  23. * Menu:
  24. * Overview::        Preliminary information.
  25. * Comparison::        What file comparison means.
  26. * Output Formats::    Formats for difference reports.
  27. * Comparing Directories::    Comparing files and directories.
  28. * Adjusting Output::    Making `diff' output prettier.
  29. * diff Performance::    Making `diff' smarter or faster.
  30. * Comparing Three Files:: Formats for three-way difference reports.
  31. * diff3 Merging::    Merging from a common ancestor.
  32. * Interactive Merging::    Interactive merging with `sdiff'.
  33. * Merging with patch::    Using `patch' to change old files into new ones.
  34. * Making Patches::    Tips for making patch distributions.
  35. * Invoking cmp::    How to run `cmp' and a summary of its options.
  36. * Invoking diff::    How to run `diff' and a summary of its options.
  37. * Invoking diff3::    How to run `diff3' and a summary of its options.
  38. * Invoking patch::    How to run `patch' and a summary of its options.
  39. * Invoking sdiff::    How to run `sdiff' and a summary of its options.
  40. * Incomplete Lines::    Lines that lack trailing newlines.
  41. * Projects::        If you think you've found a bug or other shortcoming.
  42. * Concept Index::    Index of concepts.
  43. File: diff.info,  Node: Overview,  Next: Comparison,  Up: Top
  44. Overview
  45. ********
  46.    Computer users often find occasion to ask how two files differ.
  47. Perhaps one file is a newer version of the other file.  Or maybe the
  48. two files started out as identical copies but were changed by different
  49. people.
  50.    You can use the `diff' command to show differences between two
  51. files, or each corresponding file in two directories.  `diff' outputs
  52. differences between files line by line in any of several formats,
  53. selectable by command line options.  This set of differences is often
  54. called a "diff" or "patch".  For files that are identical, `diff'
  55. normally produces no output; for binary (non-text) files, `diff'
  56. normally reports only that they are different.
  57.    You can use the `cmp' command to show the offsets and line numbers
  58. where two files differ.  `cmp' can also show all the characters that
  59. differ between the two files, side by side.  Another way to compare two
  60. files character by character is the Emacs command `M-x
  61. compare-windows'.  *Note Other Window: (emacs)Other Window, for more
  62. information on that command.
  63.    You can use the `diff3' command to show differences among three
  64. files.  When two people have made independent changes to a common
  65. original, `diff3' can report the differences between the original and
  66. the two changed versions, and can produce a merged file that contains
  67. both persons' changes together with warnings about conflicts.
  68.    You can use the `sdiff' command to merge two files interactively.
  69.    You can use the set of differences produced by `diff' to distribute
  70. updates to text files (such as program source code) to other people.
  71. This method is especially useful when the differences are small compared
  72. to the complete files.  Given `diff' output, you can use the `patch'
  73. program to update, or "patch", a copy of the file.  If you think of
  74. `diff' as subtracting one file from another to produce their
  75. difference, you can think of `patch' as adding the difference to one
  76. file to reproduce the other.
  77.    This manual first concentrates on making diffs, and later shows how
  78. to use diffs to update files.
  79.    GNU `diff' was written by Mike Haertel, David Hayes, Richard
  80. Stallman, Len Tower, and Paul Eggert.  Wayne Davison designed and
  81. implemented the unified output format.  The basic algorithm is described
  82. in "An O(ND) Difference Algorithm and its Variations", Eugene Myers,
  83. `Algorithmica' Vol. 1 No. 2, 1986, p. 251; and in "A File Comparison
  84. Program", W. Miller and E. Myers, `Software Practice and Experience'
  85. Vol. 15 No. 11, 1985, p. 1025.
  86.    GNU `diff3' was written by Randy Smith.  GNU `sdiff' was written by
  87. Thomas Lord.  GNU `cmp' was written by Torbjorn Granlund and David
  88. MacKenzie.
  89.    `patch' was written mainly by Larry Wall; the GNU enhancements were
  90. written mainly by Wayne Davison and David MacKenzie.  Parts of this
  91. manual are adapted from a manual page written by Larry Wall, with his
  92. permission.
  93. File: diff.info,  Node: Comparison,  Next: Output Formats,  Prev: Overview,  Up: Top
  94. What Comparison Means
  95. *********************
  96.    There are several ways to think about the differences between two
  97. files.  One way to think of the differences is as a series of lines
  98. that were deleted from, inserted in, or changed in one file to produce
  99. the other file.  `diff' compares two files line by line, finds groups of
  100. lines that differ, and reports each group of differing lines.  It can
  101. report the differing lines in several formats, which have different
  102. purposes.
  103.    GNU `diff' can show whether files are different without detailing
  104. the differences.  It also provides ways to suppress certain kinds of
  105. differences that are not important to you.  Most commonly, such
  106. differences are changes in the amount of whitespace between words or
  107. lines.  `diff' also provides ways to suppress differences in alphabetic
  108. case or in lines that match a regular expression that you provide.
  109. These options can accumulate; for example, you can ignore changes in
  110. both whitespace and alphabetic case.
  111.    Another way to think of the differences between two files is as a
  112. sequence of pairs of characters that can be either identical or
  113. different.  `cmp' reports the differences between two files character
  114. by character, instead of line by line.  As a result, it is more useful
  115. than `diff' for comparing binary files.  For text files, `cmp' is
  116. useful mainly when you want to know only whether two files are
  117. identical.  For this purpose, it is better than `diff' because it is
  118. much faster.
  119.    To illustrate the effect that considering changes character by
  120. character can have compared with considering them line by line, think
  121. of what happens if a single newline character is added to the beginning
  122. of a file.  If that file is then compared with an otherwise identical
  123. file that lacks the newline at the beginning, `diff' will report that a
  124. blank line has been added to the file, while `cmp' will report that
  125. almost every character of the two files differs.
  126.    `diff3' normally compares three input files line by line, finds
  127. groups of lines that differ, and reports each group of differing lines.
  128. Its output is designed to make it easy to inspect two different sets of
  129. changes to the same file.
  130. * Menu:
  131. * Hunks::        Groups of differing lines.
  132. * Whitespace::        Suppressing differences in whitespace.
  133. * Blank Lines::        Suppressing differences in blank lines.
  134. * Case Folding::    Suppressing differences in alphabetic case.
  135. * Specified Folding::    Suppressing differences that match regular expressions.
  136. * Brief::        Summarizing which files are different.
  137. * Binary::        Comparing binary files or forcing text comparisons.
  138. File: diff.info,  Node: Hunks,  Next: Whitespace,  Up: Comparison
  139. Hunks
  140. =====
  141.    When comparing two files, `diff' finds sequences of lines common to
  142. both files, interspersed with groups of differing lines called "hunks".
  143. Comparing two identical files yields one sequence of common lines and
  144. no hunks, because no lines differ.  Comparing two entirely different
  145. files yields no common lines and one large hunk that contains all lines
  146. of both files.  In general, there are many ways to match up lines
  147. between two given files.  `diff' tries to minimize the total hunk size
  148. by finding large sequences of common lines interspersed with small
  149. hunks of differing lines.
  150.    For example, suppose the file `F' contains the three lines `a', `b',
  151. `c', and the file `G' contains the same three lines in reverse order
  152. `c', `b', `a'.  If `diff' finds the line `c' as common, then the command
  153. `diff F G' produces this output:
  154.      1,2d0
  155.      < a
  156.      < b
  157.      3a2,3
  158.      > b
  159.      > a
  160. But if `diff' notices the common line `b' instead, it produces this
  161. output:
  162.      1c1
  163.      < a
  164.      ---
  165.      > c
  166.      3c3
  167.      < c
  168.      ---
  169.      > a
  170. It is also possible to find `a' as the common line.  `diff' does not
  171. always find an optimal matching between the files; it takes shortcuts
  172. to run faster.  But its output is usually close to the shortest
  173. possible.  You can adjust this tradeoff with the `--minimal' option
  174. (*note diff Performance::.).
  175. File: diff.info,  Node: Whitespace,  Next: Blank Lines,  Prev: Hunks,  Up: Comparison
  176. Suppressing Differences in Blank and Tab Spacing
  177. ================================================
  178.    The `-b' and `--ignore-space-change' options ignore blanks and tabs
  179. at line end, and to consider all other sequences of one or more blank
  180. and tab characters to be equivalent.  With these options, `diff'
  181. considers the following two lines to be equivalent, where `$' denotes
  182. the line end:
  183.      Here lyeth  muche rychnesse  in lytell space.   -- John Heywood$
  184.      Here lyeth muche rychnesse in lytell space. -- John Heywood   $
  185.    The `-w' and `--ignore-all-space' options are stronger than `-b'.
  186. They ignore difference even if one file has whitespace where the other
  187. file has none, and they ignore all whitespace characters, not just
  188. blanks and tabs.  (The "whitespace" characters include backspace, tab,
  189. vertical tab, formfeed, carriage return, space, and no-break space.)
  190. With these options, `diff' considers the following two lines to be
  191. equivalent, where `$' denotes the line end and `^M' denotes a carriage
  192. return:
  193.      Here lyeth  muche  rychnesse in lytell space.--  John Heywood$
  194.        He relyeth much erychnes  seinly tells pace.  --John Heywood   ^M$
  195. File: diff.info,  Node: Blank Lines,  Next: Case Folding,  Prev: Whitespace,  Up: Comparison
  196. Suppressing Differences in Blank Lines
  197. ======================================
  198.    The `-B' and `--ignore-blank-lines' options ignore insertions or
  199. deletions of blank lines.  These options normally affect only lines
  200. that are completely empty; they do not affect lines that look empty but
  201. contain space or tab characters.  With these options, for example, a
  202. file containing
  203.      1.  A point is that which has no part.
  204.      
  205.      2.  A line is breadthless length.
  206.      -- Euclid, The Elements, I
  207. is considered identical to a file containing
  208.      1.  A point is that which has no part.
  209.      2.  A line is breadthless length.
  210.      
  211.      
  212.      -- Euclid, The Elements, I
  213. File: diff.info,  Node: Case Folding,  Next: Specified Folding,  Prev: Blank Lines,  Up: Comparison
  214. Suppressing Case Differences
  215. ============================
  216.    GNU `diff' can treat lowercase letters as equivalent to their
  217. uppercase counterparts, so that, for example, it considers `Funky
  218. Stuff', `funky STUFF', and `fUNKy stuFf' to all be the same.  To
  219. request this, use the `-i' or `--ignore-case' option.
  220. File: diff.info,  Node: Specified Folding,  Next: Brief,  Prev: Case Folding,  Up: Comparison
  221. Suppressing Lines Matching a Regular Expression
  222. ===============================================
  223.    To ignore insertions and deletions of lines that match a regular
  224. expression, use the `-I REGEXP' or `--ignore-matching-lines=REGEXP'
  225. option.  You should escape regular expressions that contain shell
  226. metacharacters to prevent the shell from expanding them.  For example,
  227. `diff -I '^[0-9]'' ignores all changes to lines beginning with a digit.
  228.    However, `-I' only ignores the insertion or deletion of lines that
  229. contain the regular expression if every changed line in the hunk--every
  230. insertion and every deletion--matches the regular expression.  In other
  231. words, for each nonignorable change, `diff' prints the complete set of
  232. changes in its vicinity, including the ignorable ones.
  233.    You can specify more than one regular expression for lines to ignore
  234. by using more than one `-I' option.  `diff' tries to match each line
  235. against each regular expression, starting with the last one given.
  236. File: diff.info,  Node: Brief,  Next: Binary,  Prev: Specified Folding,  Up: Comparison
  237. Summarizing Which Files Differ
  238. ==============================
  239.    When you only want to find out whether files are different, and you
  240. don't care what the differences are, you can use the summary output
  241. format.  In this format, instead of showing the differences between the
  242. files, `diff' simply reports whether files differ.  The `-q' and
  243. `--brief' options select this output format.
  244.    This format is especially useful when comparing the contents of two
  245. directories.  It is also much faster than doing the normal line by line
  246. comparisons, because `diff' can stop analyzing the files as soon as it
  247. knows that there are any differences.
  248.    You can also get a brief indication of whether two files differ by
  249. using `cmp'.  For files that are identical, `cmp' produces no output.
  250. When the files differ, by default, `cmp' outputs the byte offset and
  251. line number where the first difference occurs.  You can use the `-s'
  252. option to suppress that information, so that `cmp' produces no output
  253. and reports whether the files differ using only its exit status (*note
  254. Invoking cmp::.).
  255.    Unlike `diff', `cmp' cannot compare directories; it can only compare
  256. two files.
  257. File: diff.info,  Node: Binary,  Prev: Brief,  Up: Comparison
  258. Binary Files and Forcing Text Comparisons
  259. =========================================
  260.    If `diff' thinks that either of the two files it is comparing is
  261. binary (a non-text file), it normally treats that pair of files much as
  262. if the summary output format had been selected (*note Brief::.), and
  263. reports only that the binary files are different.  This is because line
  264. by line comparisons are usually not meaningful for binary files.
  265.    `diff' determines whether a file is text or binary by checking the
  266. first few bytes in the file; the exact number of bytes is system
  267. dependent, but it is typically several thousand.  If every character in
  268. that part of the file is non-null, `diff' considers the file to be
  269. text; otherwise it considers the file to be binary.
  270.    Sometimes you might want to force `diff' to consider files to be
  271. text.  For example, you might be comparing text files that contain null
  272. characters; `diff' would erroneously decide that those are non-text
  273. files.  Or you might be comparing documents that are in a format used
  274. by a word processing system that uses null characters to indicate
  275. special formatting.  You can force `diff' to consider all files to be
  276. text files, and compare them line by line, by using the `-a' or
  277. `--text' option.  If the files you compare using this option do not in
  278. fact contain text, they will probably contain few newline characters,
  279. and the `diff' output will consist of hunks showing differences between
  280. long lines of whatever characters the files contain.
  281.    You can also force `diff' to consider all files to be binary files,
  282. and report only whether they differ (but not how).  Use the `--brief'
  283. option for this.
  284.    If you want to compare two files byte by byte, you can use the `cmp'
  285. program with the `-l' option to show the values of each differing byte
  286. in the two files.  With GNU `cmp', you can also use the `-c' option to
  287. show the ASCII representation of those bytes.  *Note Invoking cmp::,
  288. for more information.
  289.    If `diff3' thinks that any of the files it is comparing is binary (a
  290. non-text file), it normally reports an error, because such comparisons
  291. are usually not useful.  `diff3' uses the same test as `diff' to decide
  292. whether a file is binary.  As with `diff', if the input files contain a
  293. few non-text characters but otherwise are like text files, you can
  294. force `diff3' to consider all files to be text files and compare them
  295. line by line by using the `-a' or `--text' options.
  296. File: diff.info,  Node: Output Formats,  Next: Comparing Directories,  Prev: Comparison,  Up: Top
  297. `diff' Output Formats
  298. *********************
  299.    `diff' has several mutually exclusive options for output format.
  300. The following sections describe each format, illustrating how `diff'
  301. reports the differences between two sample input files.
  302. * Menu:
  303. * Sample diff Input::    Sample `diff' input files for examples.
  304. * Normal::        Showing differences without surrounding text.
  305. * Context::        Showing differences with the surrounding text.
  306. * Side by Side::        Showing differences in two columns.
  307. * Scripts::        Generating scripts for other programs.
  308. * If-then-else::    Merging files with if-then-else.
  309. File: diff.info,  Node: Sample diff Input,  Next: Normal,  Up: Output Formats
  310. Two Sample Input Files
  311. ======================
  312.    Here are two sample files that we will use in numerous examples to
  313. illustrate the output of `diff' and how various options can change it.
  314.    This is the file `lao':
  315.      The Way that can be told of is not the eternal Way;
  316.      The name that can be named is not the eternal name.
  317.      The Nameless is the origin of Heaven and Earth;
  318.      The Named is the mother of all things.
  319.      Therefore let there always be non-being,
  320.        so we may see their subtlety,
  321.      And let there always be being,
  322.        so we may see their outcome.
  323.      The two are the same,
  324.      But after they are produced,
  325.        they have different names.
  326.    This is the file `tzu':
  327.      The Nameless is the origin of Heaven and Earth;
  328.      The named is the mother of all things.
  329.      
  330.      Therefore let there always be non-being,
  331.        so we may see their subtlety,
  332.      And let there always be being,
  333.        so we may see their outcome.
  334.      The two are the same,
  335.      But after they are produced,
  336.        they have different names.
  337.      They both may be called deep and profound.
  338.      Deeper and more profound,
  339.      The door of all subtleties!
  340.    In this example, the first hunk contains just the first two lines of
  341. `lao', the second hunk contains the fourth line of `lao' opposing the
  342. second and third lines of `tzu', and the last hunk contains just the
  343. last three lines of `tzu'.
  344. File: diff.info,  Node: Normal,  Next: Context,  Prev: Sample diff Input,  Up: Output Formats
  345. Showing Differences Without Context
  346. ===================================
  347.    The "normal" `diff' output format shows each hunk of differences
  348. without any surrounding context.  Sometimes such output is the clearest
  349. way to see how lines have changed, without the clutter of nearby
  350. unchanged lines (although you can get similar results with the context
  351. or unified formats by using 0 lines of context).  However, this format
  352. is no longer widely used for sending out patches; for that purpose, the
  353. context format (*note Context Format::.) and the unified format (*note
  354. Unified Format::.) are superior.  Normal format is the default for
  355. compatibility with older versions of `diff' and the POSIX standard.
  356. * Menu:
  357. * Detailed Normal::    A detailed description of normal output format.
  358. * Example Normal::    Sample output in the normal format.
  359. File: diff.info,  Node: Detailed Normal,  Next: Example Normal,  Up: Normal
  360. Detailed Description of Normal Format
  361. -------------------------------------
  362.    The normal output format consists of one or more hunks of
  363. differences; each hunk shows one area where the files differ.  Normal
  364. format hunks look like this:
  365.      CHANGE-COMMAND
  366.      < FROM-FILE-LINE
  367.      < FROM-FILE-LINE...
  368.      ---
  369.      > TO-FILE-LINE
  370.      > TO-FILE-LINE...
  371.    There are three types of change commands.  Each consists of a line
  372. number or comma-separated range of lines in the first file, a single
  373. character indicating the kind of change to make, and a line number or
  374. comma-separated range of lines in the second file.  All line numbers are
  375. the original line numbers in each file.  The types of change commands
  376. `LaR'
  377.      Add the lines in range R of the second file after line L of the
  378.      first file.  For example, `8a12,15' means append lines 12-15 of
  379.      file 2 after line 8 of file 1; or, if changing file 2 into file 1,
  380.      delete lines 12-15 of file 2.
  381. `FcT'
  382.      Replace the lines in range F of the first file with lines in range
  383.      T of the second file.  This is like a combined add and delete, but
  384.      more compact.  For example, `5,7c8,10' means change lines 5-7 of
  385.      file 1 to read as lines 8-10 of file 2; or, if changing file 2 into
  386.      file 1, change lines 8-10 of file 2 to read as lines 5-7 of file 1.
  387. `RdL'
  388.      Delete the lines in range R from the first file; line L is where
  389.      they would have appeared in the second file had they not been
  390.      deleted.  For example, `5,7d3' means delete lines 5-7 of file 1;
  391.      or, if changing file 2 into file 1, append lines 5-7 of file 1
  392.      after line 3 of file 2.
  393. File: diff.info,  Node: Example Normal,  Prev: Detailed Normal,  Up: Normal
  394. An Example of Normal Format
  395. ---------------------------
  396.    Here is the output of the command `diff lao tzu' (*note Sample diff
  397. Input::., for the complete contents of the two files).  Notice that it
  398. shows only the lines that are different between the two files.
  399.      1,2d0
  400.      < The Way that can be told of is not the eternal Way;
  401.      < The name that can be named is not the eternal name.
  402.      4c2,3
  403.      < The Named is the mother of all things.
  404.      ---
  405.      > The named is the mother of all things.
  406.      >
  407.      11a11,13
  408.      > They both may be called deep and profound.
  409.      > Deeper and more profound,
  410.      > The door of all subtleties!
  411. File: diff.info,  Node: Context,  Next: Side by Side,  Prev: Normal,  Up: Output Formats
  412. Showing Differences in Their Context
  413. ====================================
  414.    Usually, when you are looking at the differences between files, you
  415. will also want to see the parts of the files near the lines that
  416. differ, to help you understand exactly what has changed.  These nearby
  417. parts of the files are called the "context".
  418.    GNU `diff' provides two output formats that show context around the
  419. differing lines: "context format" and "unified format".  It can
  420. optionally show in which function or section of the file the differing
  421. lines are found.
  422.    If you are distributing new versions of files to other people in the
  423. form of `diff' output, you should use one of the output formats that
  424. show context so that they can apply the diffs even if they have made
  425. small changes of their own to the files.  `patch' can apply the diffs
  426. in this case by searching in the files for the lines of context around
  427. the differing lines; if those lines are actually a few lines away from
  428. where the diff says they are, `patch' can adjust the line numbers
  429. accordingly and still apply the diff correctly.  *Note Imperfect::, for
  430. more information on using `patch' to apply imperfect diffs.
  431. * Menu:
  432. * Context Format::    An output format that shows surrounding lines.
  433. * Unified Format::    A more compact output format that shows context.
  434. * Sections::        Showing which sections of the files differences are in.
  435. * Alternate Names::    Showing alternate file names in context headers.
  436. File: diff.info,  Node: Context Format,  Next: Unified Format,  Up: Context
  437. Context Format
  438. --------------
  439.    The context output format shows several lines of context around the
  440. lines that differ.  It is the standard format for distributing updates
  441. to source code.
  442.    To select this output format, use the `-C LINES',
  443. `--context[=LINES]', or `-c' option.  The argument LINES that some of
  444. these options take is the number of lines of context to show.  If you
  445. do not specify LINES, it defaults to three.
  446. * Menu:
  447. * Detailed Context::    A detailed description of the context output format.
  448. * Example Context::    Sample output in context format.
  449. * Less Context::    Another sample with less context.
  450. File: diff.info,  Node: Detailed Context,  Next: Example Context,  Up: Context Format
  451. Detailed Description of Context Format
  452. ......................................
  453.    The context output format starts with a two-line header, which looks
  454. like this:
  455.      *** FROM-FILE FROM-FILE-MODIFICATION-TIME
  456.      --- TO-FILE TO-FILE-MODIFICATION TIME
  457. You can change the header's content with the `-L LABEL' or
  458. `--label=LABEL' option; see *Note Alternate Names::.
  459.    Next come one or more hunks of differences; each hunk shows one area
  460. where the files differ.  Context format hunks look like this:
  461.      ***************
  462.      *** FROM-FILE-LINE-RANGE ****
  463.        FROM-FILE-LINE
  464.        FROM-FILE-LINE...
  465.      --- TO-FILE-LINE-RANGE ----
  466.        TO-FILE-LINE
  467.        TO-FILE-LINE...
  468.    The lines of context around the lines that differ start with two
  469. space characters.  The lines that differ between the two files start
  470. with one of the following indicator characters, followed by a space
  471. character:
  472.      A line that is part of a group of one or more lines that changed
  473.      between the two files.  There is a corresponding group of lines
  474.      marked with `!' in the part of this hunk for the other file.
  475.      An "inserted" line in the second file that corresponds to nothing
  476.      in the first file.
  477.      A "deleted" line in the first file that corresponds to nothing in
  478.      the second file.
  479.    If all of the changes in a hunk are insertions, the lines of
  480. FROM-FILE are omitted.  If all of the changes are deletions, the lines
  481. of TO-FILE are omitted.
  482. File: diff.info,  Node: Example Context,  Next: Less Context,  Prev: Detailed Context,  Up: Context Format
  483. An Example of Context Format
  484. ............................
  485.    Here is the output of `diff -c lao tzu' (*note Sample diff Input::.,
  486. for the complete contents of the two files).  Notice that up to three
  487. lines that are not different are shown around each line that is
  488. different; they are the context lines.  Also notice that the first two
  489. hunks have run together, because their contents overlap.
  490.      *** lao    Sat Jan 26 23:30:39 1991
  491.      --- tzu    Sat Jan 26 23:30:50 1991
  492.      ***************
  493.      *** 1,7 ****
  494.      - The Way that can be told of is not the eternal Way;
  495.      - The name that can be named is not the eternal name.
  496.        The Nameless is the origin of Heaven and Earth;
  497.      ! The Named is the mother of all things.
  498.        Therefore let there always be non-being,
  499.          so we may see their subtlety,
  500.        And let there always be being,
  501.      --- 1,6 ----
  502.        The Nameless is the origin of Heaven and Earth;
  503.      ! The named is the mother of all things.
  504.      !
  505.        Therefore let there always be non-being,
  506.          so we may see their subtlety,
  507.        And let there always be being,
  508.      ***************
  509.      *** 9,11 ****
  510.      --- 8,13 ----
  511.        The two are the same,
  512.        But after they are produced,
  513.          they have different names.
  514.      + They both may be called deep and profound.
  515.      + Deeper and more profound,
  516.      + The door of all subtleties!
  517. File: diff.info,  Node: Less Context,  Prev: Example Context,  Up: Context Format
  518. An Example of Context Format with Less Context
  519. ..............................................
  520.    Here is the output of `diff --context=1 lao tzu' (*note Sample diff
  521. Input::., for the complete contents of the two files).  Notice that at
  522. most one context line is reported here.
  523.      *** lao    Sat Jan 26 23:30:39 1991
  524.      --- tzu    Sat Jan 26 23:30:50 1991
  525.      ***************
  526.      *** 1,5 ****
  527.      - The Way that can be told of is not the eternal Way;
  528.      - The name that can be named is not the eternal name.
  529.        The Nameless is the origin of Heaven and Earth;
  530.      ! The Named is the mother of all things.
  531.        Therefore let there always be non-being,
  532.      --- 1,4 ----
  533.        The Nameless is the origin of Heaven and Earth;
  534.      ! The named is the mother of all things.
  535.      !
  536.        Therefore let there always be non-being,
  537.      ***************
  538.      *** 11 ****
  539.      --- 10,13 ----
  540.          they have different names.
  541.      + They both may be called deep and profound.
  542.      + Deeper and more profound,
  543.      + The door of all subtleties!
  544. File: diff.info,  Node: Unified Format,  Next: Sections,  Prev: Context Format,  Up: Context
  545. Unified Format
  546. --------------
  547.    The unified output format is a variation on the context format that
  548. is more compact because it omits redundant context lines.  To select
  549. this output format, use the `-U LINES', `--unified[=LINES]', or `-u'
  550. option.  The argument LINES is the number of lines of context to show.
  551. When it is not given, it defaults to three.
  552.    At present, only GNU `diff' can produce this format and only GNU
  553. `patch' can automatically apply diffs in this format.
  554. * Menu:
  555. * Detailed Unified::    A detailed description of unified format.
  556. * Example Unified::    Sample output in unified format.
  557. File: diff.info,  Node: Detailed Unified,  Next: Example Unified,  Up: Unified Format
  558. Detailed Description of Unified Format
  559. ......................................
  560.    The unified output format starts with a two-line header, which looks
  561. like this:
  562.      --- FROM-FILE FROM-FILE-MODIFICATION-TIME
  563.      +++ TO-FILE TO-FILE-MODIFICATION-TIME
  564. You can change the header's content with the `-L LABEL' or
  565. `--label=LABEL' option; see *Note Alternate Names::.
  566.    Next come one or more hunks of differences; each hunk shows one area
  567. where the files differ.  Unified format hunks look like this:
  568.      @@ FROM-FILE-RANGE TO-FILE-RANGE @@
  569.       LINE-FROM-EITHER-FILE
  570.       LINE-FROM-EITHER-FILE...
  571.    The lines common to both files begin with a space character.  The
  572. lines that actually differ between the two files have one of the
  573. following indicator characters in the left column:
  574.      A line was added here to the first file.
  575.      A line was removed here from the first file.
  576. File: diff.info,  Node: Example Unified,  Prev: Detailed Unified,  Up: Unified Format
  577. An Example of Unified Format
  578. ............................
  579.    Here is the output of the command `diff -u lao tzu' (*note Sample
  580. diff Input::., for the complete contents of the two files):
  581.      --- lao    Sat Jan 26 23:30:39 1991
  582.      +++ tzu    Sat Jan 26 23:30:50 1991
  583.      @@ -1,7 +1,6 @@
  584.      -The Way that can be told of is not the eternal Way;
  585.      -The name that can be named is not the eternal name.
  586.       The Nameless is the origin of Heaven and Earth;
  587.      -The Named is the mother of all things.
  588.      +The named is the mother of all things.
  589.      +
  590.       Therefore let there always be non-being,
  591.         so we may see their subtlety,
  592.       And let there always be being,
  593.      @@ -9,3 +8,6 @@
  594.       The two are the same,
  595.       But after they are produced,
  596.         they have different names.
  597.      +They both may be called deep and profound.
  598.      +Deeper and more profound,
  599.      +The door of all subtleties!
  600. File: diff.info,  Node: Sections,  Next: Alternate Names,  Prev: Unified Format,  Up: Context
  601. Showing Which Sections Differences Are in
  602. -----------------------------------------
  603.    Sometimes you might want to know which part of the files each change
  604. falls in.  If the files are source code, this could mean which function
  605. was changed.  If the files are documents, it could mean which chapter or
  606. appendix was changed.  GNU `diff' can show this by displaying the
  607. nearest section heading line that precedes the differing lines.  Which
  608. lines are "section headings" is determined by a regular expression.
  609. * Menu:
  610. * Specified Headings::    Showing headings that match regular expressions.
  611. * C Function Headings::    Showing headings of C functions.
  612. File: diff.info,  Node: Specified Headings,  Next: C Function Headings,  Up: Sections
  613. Showing Lines That Match Regular Expressions
  614. ............................................
  615.    To show in which sections differences occur for files that are not
  616. source code for C or similar languages, use the `-F REGEXP' or
  617. `--show-function-line=REGEXP' option.  `diff' considers lines that
  618. match the argument REGEXP to be the beginning of a section of the file.
  619. Here are suggested regular expressions for some common languages:
  620. `^[A-Za-z_]'
  621.      C, C++, Prolog
  622.      Lisp
  623. `^@\(chapter\|appendix\|unnumbered\|chapheading\)'
  624.      Texinfo
  625.    This option does not automatically select an output format; in order
  626. to use it, you must select the context format (*note Context Format::.)
  627. or unified format (*note Unified Format::.).  In other output formats it
  628. has no effect.
  629.    The `-F' and `--show-function-line' options find the nearest
  630. unchanged line that precedes each hunk of differences and matches the
  631. given regular expression.  Then they add that line to the end of the
  632. line of asterisks in the context format, or to the `@@' line in unified
  633. format.  If no matching line exists, they leave the output for that
  634. hunk unchanged.  If that line is more than 40 characters long, they
  635. output only the first 40 characters.  You can specify more than one
  636. regular expression for such lines; `diff' tries to match each line
  637. against each regular expression, starting with the last one given.  This
  638. means that you can use `-p' and `-F' together, if you wish.
  639. File: diff.info,  Node: C Function Headings,  Prev: Specified Headings,  Up: Sections
  640. Showing C Function Headings
  641. ...........................
  642.    To show in which functions differences occur for C and similar
  643. languages, you can use the `-p' or `--show-c-function' option.  This
  644. option automatically selects the context output format (*note Context
  645. Format::.), with the default number of lines of context.  You can
  646. override that number with `-C LINES' later in the command line.  You
  647. can override both the format and the number with `-U LINES' later in
  648. the command line.
  649.    The `-p' and `--show-c-function' options are equivalent to `-c
  650. -F'^[_a-zA-Z$]'' (*note Specified Headings::.).  GNU `diff' provides
  651. them for the sake of convenience.
  652. File: diff.info,  Node: Alternate Names,  Prev: Sections,  Up: Context
  653. Showing Alternate File Names
  654. ----------------------------
  655.    If you are comparing two files that have meaningless or uninformative
  656. names, you might want `diff' to show alternate names in the header of
  657. the context and unified output formats.  To do this, use the `-L LABEL'
  658. or `--label=LABEL' option.  The first time you give this option, its
  659. argument replaces the name and date of the first file in the header;
  660. the second time, its argument replaces the name and date of the second
  661. file.  If you give this option more than twice, `diff' reports an
  662. error.  The `-L' option does not affect the file names in the `pr'
  663. header when the `-l' or `--paginate' option is used (*note
  664. Pagination::.).
  665.    Here are the first two lines of the output from `diff -C2 -Loriginal
  666. -Lmodified lao tzu':
  667.      *** original
  668.      --- modified
  669. File: diff.info,  Node: Side by Side,  Next: Scripts,  Prev: Context,  Up: Output Formats
  670. Showing Differences Side by Side
  671. ================================
  672.    `diff' can produce a side by side difference listing of two files.
  673. The files are listed in two columns with a gutter between them.  The
  674. gutter contains one of the following markers:
  675. white space
  676.      The corresponding lines are in common.  That is, either the lines
  677.      are identical, or the difference is ignored because of one of the
  678.      `--ignore' options (*note Whitespace::.).
  679.      The corresponding lines differ, and they are either both complete
  680.      or both incomplete.
  681.      The files differ and only the first file contains the line.
  682.      The files differ and only the second file contains the line.
  683.      Only the first file contains the line, but the difference is
  684.      ignored.
  685.      Only the second file contains the line, but the difference is
  686.      ignored.
  687.      The corresponding lines differ, and only the first line is
  688.      incomplete.
  689.      The corresponding lines differ, and only the second line is
  690.      incomplete.
  691.    Normally, an output line is incomplete if and only if the lines that
  692. it contains are incomplete; *Note Incomplete Lines::.  However, when an
  693. output line represents two differing lines, one might be incomplete
  694. while the other is not.  In this case, the output line is complete, but
  695. its the gutter is marked `\' if the first line is incomplete, `/' if
  696. the second line is.
  697.    Side by side format is sometimes easiest to read, but it has
  698. limitations.  It generates much wider output than usual, and truncates
  699. lines that are too long to fit.  Also, it relies on lining up output
  700. more heavily than usual, so its output looks particularly bad if you
  701. use varying width fonts, nonstandard tab stops, or nonprinting
  702. characters.
  703.    You can use the `sdiff' command to interactively merge side by side
  704. differences.  *Note Interactive Merging::, for more information on
  705. merging files.
  706. * Menu:
  707. * Side by Side Format::        Controlling side by side output format.
  708. * Example Side by Side::    Sample side by side output.
  709. File: diff.info,  Node: Side by Side Format,  Next: Example Side by Side,  Up: Side by Side
  710. Controlling Side by Side Format
  711. ===============================
  712.    The `-y' or `--side-by-side' option selects side by side format.
  713. Because side by side output lines contain two input lines, they are
  714. wider than usual.  They are normally 130 columns, which can fit onto a
  715. traditional printer line.  You can set the length of output lines with
  716. the `-W COLUMNS' or `--width=COLUMNS' option.  The output line is split
  717. into two halves of equal length, separated by a small gutter to mark
  718. differences; the right half is aligned to a tab stop so that tabs line
  719. up.  Input lines that are too long to fit in half of an output line are
  720. truncated for output.
  721.    The `--left-column' option prints only the left column of two common
  722. lines.  The `--suppress-common-lines' option suppresses common lines
  723. entirely.
  724. File: diff.info,  Node: Example Side by Side,  Prev: Side by Side Format,  Up: Side by Side
  725. An Example of Side by Side Format
  726. ---------------------------------
  727.    Here is the output of the command `diff -y -W 72 lao tzu' (*note
  728. Sample diff Input::., for the complete contents of the two files).
  729.      The Way that can be told of is n   <
  730.      The name that can be named is no   <
  731.      The Nameless is the origin of He        The Nameless is the origin of He
  732.      The Named is the mother of all t   |    The named is the mother of all t
  733.                                         >
  734.      Therefore let there always be no        Therefore let there always be no
  735.        so we may see their subtlety,           so we may see their subtlety,
  736.      And let there always be being,          And let there always be being,
  737.        so we may see their outcome.            so we may see their outcome.
  738.      The two are the same,                   The two are the same,
  739.      But after they are produced,            But after they are produced,
  740.        they have different names.              they have different names.
  741.                                         >    They both may be called deep and
  742.                                         >    Deeper and more profound,
  743.                                         >    The door of all subtleties!
  744. File: diff.info,  Node: Scripts,  Next: If-then-else,  Prev: Side by Side,  Up: Output Formats
  745. Making Edit Scripts
  746. ===================
  747.    Several output modes produce command scripts for editing FROM-FILE
  748. to produce TO-FILE.
  749. * Menu:
  750. * ed Scripts::        Using `diff' to produce commands for `ed'.
  751. * Forward ed::        Making forward `ed' scripts.
  752. * RCS::            A special `diff' output format used by RCS.
  753. File: diff.info,  Node: ed Scripts,  Next: Forward ed,  Up: Scripts
  754. `ed' Scripts
  755. ------------
  756.    `diff' can produce commands that direct the `ed' text editor to
  757. change the first file into the second file.  Long ago, this was the
  758. only output mode that was suitable for editing one file into another
  759. automatically; today, with `patch', it is almost obsolete.  Use the
  760. `-e' or `--ed' option to select this output format.
  761.    Like the normal format (*note Normal::.), this output format does not
  762. show any context; unlike the normal format, it does not include the
  763. information necessary to apply the diff in reverse (to produce the first
  764. file if all you have is the second file and the diff).
  765.    If the file `d' contains the output of `diff -e old new', then the
  766. command `(cat d && echo w) | ed - old' edits `old' to make it a copy of
  767. `new'.  More generally, if `d1', `d2', ..., `dN' contain the outputs of
  768. `diff -e old new1', `diff -e new1 new2', ..., `diff -e newN-1 newN',
  769. respectively, then the command `(cat d1 d2 ... dN && echo w) | ed -
  770. old' edits `old' to make it a copy of `newN'.
  771. * Menu:
  772. * Detailed ed::        A detailed description of `ed' format.
  773. * Example ed::        A sample `ed' script.
  774. File: diff.info,  Node: Detailed ed,  Next: Example ed,  Up: ed Scripts
  775. Detailed Description of `ed' Format
  776. ...................................
  777.    The `ed' output format consists of one or more hunks of differences.
  778. The changes closest to the ends of the files come first so that
  779. commands that change the number of lines do not affect how `ed'
  780. interprets line numbers in succeeding commands.  `ed' format hunks look
  781. like this:
  782.      CHANGE-COMMAND
  783.      TO-FILE-LINE
  784.      TO-FILE-LINE...
  785.      .
  786.    Because `ed' uses a single period on a line to indicate the end of
  787. input, GNU `diff' protects lines of changes that contain a single
  788. period on a line by writing two periods instead, then writing a
  789. subsequent `ed' command to change the two periods into one.  The `ed'
  790. format cannot represent an incomplete line, so if the second file ends
  791. in a changed incomplete line, `diff' reports an error and then pretends
  792. that a newline was appended.
  793.    There are three types of change commands.  Each consists of a line
  794. number or comma-separated range of lines in the first file and a single
  795. character indicating the kind of change to make.  All line numbers are
  796. the original line numbers in the file.  The types of change commands
  797.      Add text from the second file after line L in the first file.  For
  798.      example, `8a' means to add the following lines after line 8 of file
  799.      1.
  800.      Replace the lines in range R in the first file with the following
  801.      lines.  Like a combined add and delete, but more compact.  For
  802.      example, `5,7c' means change lines 5-7 of file 1 to read as the
  803.      text file 2.
  804.      Delete the lines in range R from the first file.  For example,
  805.      `5,7d' means delete lines 5-7 of file 1.
  806. File: diff.info,  Node: Example ed,  Prev: Detailed ed,  Up: ed Scripts
  807. Example `ed' Script
  808. ...................
  809.    Here is the output of `diff -e lao tzu' (*note Sample diff Input::.,
  810. for the complete contents of the two files):
  811.      11a
  812.      They both may be called deep and profound.
  813.      Deeper and more profound,
  814.      The door of all subtleties!
  815.      .
  816.      4c
  817.      The named is the mother of all things.
  818.      
  819.      .
  820.      1,2d
  821. File: diff.info,  Node: Forward ed,  Next: RCS,  Prev: ed Scripts,  Up: Scripts
  822. Forward `ed' Scripts
  823. --------------------
  824.    `diff' can produce output that is like an `ed' script, but with
  825. hunks in forward (front to back) order.  The format of the commands is
  826. also changed slightly: command characters precede the lines they
  827. modify, spaces separate line numbers in ranges, and no attempt is made
  828. to disambiguate hunk lines consisting of a single period.  Like `ed'
  829. format, forward `ed' format cannot represent incomplete lines.
  830.    Forward `ed' format is not very useful, because neither `ed' nor
  831. `patch' can apply diffs in this format.  It exists mainly for
  832. compatibility with older versions of `diff'.  Use the `-f' or
  833. `--forward-ed' option to select it.
  834. File: diff.info,  Node: RCS,  Prev: Forward ed,  Up: Scripts
  835. RCS Scripts
  836. -----------
  837.    The RCS output format is designed specifically for use by the
  838. Revision Control System, which is a set of free programs used for
  839. organizing different versions and systems of files.  Use the `-n' or
  840. `--rcs' option to select this output format.  It is like the forward
  841. `ed' format (*note Forward ed::.), but it can represent arbitrary
  842. changes to the contents of a file because it avoids the forward `ed'
  843. format's problems with lines consisting of a single period and with
  844. incomplete lines.  Instead of ending text sections with a line
  845. consisting of a single period, each command specifies the number of
  846. lines it affects; a combination of the `a' and `d' commands are used
  847. instead of `c'.  Also, if the second file ends in a changed incomplete
  848. line, then the output also ends in an incomplete line.
  849.    Here is the output of `diff -n lao tzu' (*note Sample diff Input::.,
  850. for the complete contents of the two files):
  851.      d1 2
  852.      d4 1
  853.      a4 2
  854.      The named is the mother of all things.
  855.      
  856.      a11 3
  857.      They both may be called deep and profound.
  858.      Deeper and more profound,
  859.      The door of all subtleties!
  860. File: diff.info,  Node: If-then-else,  Prev: Scripts,  Up: Output Formats
  861. Merging Files with If-then-else
  862. ===============================
  863.    You can use `diff' to merge two files of C source code.  The output
  864. of `diff' in this format contains all the lines of both files.  Lines
  865. common to both files are output just once; the differing parts are
  866. separated by the C preprocessor directives `#ifdef NAME' or `#ifndef
  867. NAME', `#else', and `#endif'.  When compiling the output, you select
  868. which version to use by either defining or leaving undefined the macro
  869. NAME.
  870.    To merge two files, use `diff' with the `-D NAME' or `--ifdef=NAME'
  871. option.  The argument NAME is the C preprocessor identifier to use in
  872. the `#ifdef' and `#ifndef' directives.
  873.    For example, if you change an instance of `wait (&s)' to `waitpid
  874. (-1, &s, 0)' and then merge the old and new files with the
  875. `--ifdef=HAVE_WAITPID' option, then the affected part of your code
  876. might look like this:
  877.          do {
  878.      #ifndef HAVE_WAITPID
  879.              if ((w = wait (&s)) < 0  &&  errno != EINTR)
  880.      #else /* HAVE_WAITPID */
  881.              if ((w = waitpid (-1, &s, 0)) < 0  &&  errno != EINTR)
  882.      #endif /* HAVE_WAITPID */
  883.                  return w;
  884.          } while (w != child);
  885.    You can specify formats for languages other than C by using line
  886. group formats and line formats, as described in the next sections.
  887. * Menu:
  888. * Line Group Formats::        Formats for general if-then-else line groups.
  889. * Line Formats::        Formats for each line in a line group.
  890. * Detailed If-then-else::    A detailed description of if-then-else format.
  891. * Example If-then-else::    Sample if-then-else format output.
  892. File: diff.info,  Node: Line Group Formats,  Next: Line Formats,  Up: If-then-else
  893. Line Group Formats
  894. ------------------
  895.    Line group formats let you specify formats suitable for many
  896. applications that allow if-then-else input, including programming
  897. languages and text formatting languages.  A line group format specifies
  898. the output format for a contiguous group of similar lines.
  899.    For example, the following command compares the TeX files `old' and
  900. `new', and outputs a merged file in which old regions are surrounded by
  901. `\begin{em}'-`\end{em}' lines, and new regions are surrounded by
  902. `\begin{bf}'-`\end{bf}' lines.
  903.      diff \
  904.         --old-group-format='\begin{em}
  905.      %<\end{em}
  906.      ' \
  907.         --new-group-format='\begin{bf}
  908.      %>\end{bf}
  909.      ' \
  910.         old new
  911.    The following command is equivalent to the above example, but it is a
  912. little more verbose, because it spells out the default line group
  913. formats.
  914.      diff \
  915.         --old-group-format='\begin{em}
  916.      %<\end{em}
  917.      ' \
  918.         --new-group-format='\begin{bf}
  919.      %>\end{bf}
  920.      ' \
  921.         --unchanged-group-format='%=' \
  922.         --changed-group-format='\begin{em}
  923.      %<\end{em}
  924.      \begin{bf}
  925.      %>\end{bf}
  926.      ' \
  927.         old new
  928.    To specify a line group format, use `diff' with one of the options
  929. listed below.  You can specify up to four line group formats, one for
  930. each kind of line group.  You should quote FORMAT, because it typically
  931. contains shell metacharacters.
  932. `--old-group-format=FORMAT'
  933.      These line groups are hunks containing only lines from the first
  934.      file.  The default old group format is the same as the changed
  935.      group format if it is specified; otherwise it a format that
  936.      outputs the line group as-is.
  937. `--new-group-format=FORMAT'
  938.      These line groups are hunks containing only lines from the second
  939.      file.  The default new group format is same as the the changed
  940.      group format if it is specified; otherwise it is a format that
  941.      outputs the line group as-is.
  942. `--changed-group-format=FORMAT'
  943.      These line groups are hunks containing lines from both files.  The
  944.      default changed group format is the concatenation of the old and
  945.      new group formats.
  946. `--unchanged-group-format=FORMAT'
  947.      These line groups contain lines common to both files.  The default
  948.      unchanged group format is a format that outputs the line group
  949.      as-is.
  950.    In a line group format, ordinary characters represent themselves;
  951. conversion specifications start with `%' and have one of the following
  952. forms.
  953.      stands for the lines from the first file, including the trailing
  954.      newline.  Each line is formatted according to the old line format
  955.      (*note Line Formats::.).
  956.      stands for the lines from the second file, including the trailing
  957.      newline.  Each line is formatted according to the new line format.
  958.      stands for the lines common to both files, including the trailing
  959.      newline.  Each line is formatted according to the unchanged line
  960.      format.
  961.      stands for a null character.
  962.      stands for `%'.
  963.